package com.movile.checkhoras.android;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.ImageButton;
import android.widget.RemoteViews;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.movile.checkhoras.android.dao.TableCheckHorasHelper;
import com.movile.checkhoras.android.vo.CheckHora;
import com.movile.checkhoras.android.widget.CheckWidget1Provider;
import com.movile.checkhoras.android.widget.UpdateWidgetService;

public class CheckManualActivity extends Activity {
    
    ImageButton btCheckOutManual;
    ImageButton btCheckInManual;
    TextView txtCheckOutManual;
    TextView txtCheckInManual;
    
    TableCheckHorasHelper tableCheckHoras;
    
    DatePicker datePikcer1;
    TimePicker timePicker2;
    
    @Override
    public void onCreate(Bundle savedInstance){
        super.onCreate(savedInstance);
        setContentView(R.layout.checkmanual);
        
        btCheckOutManual = (ImageButton) findViewById(R.id.btCheckOutManual);
        btCheckInManual = (ImageButton) findViewById(R.id.btCheckInManual);
        datePikcer1 = (DatePicker) findViewById(R.id.datePicker1);
        timePicker2 = (TimePicker) findViewById(R.id.timePicker1);
        txtCheckOutManual = (TextView) findViewById(R.id.txtCheckOutManual);
        txtCheckInManual = (TextView) findViewById(R.id.txtCheckInManual);
        
        tableCheckHoras = new TableCheckHorasHelper(getBaseContext());
        
        //updateCheck();
        
        btCheckOutManual.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                datePikcer1.clearFocus();
                timePicker2.clearFocus();
                btCheckOutManual.requestFocus();
                insertCheck(false);
                
            }
        });
        
        
        btCheckInManual.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                
                datePikcer1.clearFocus();
                timePicker2.clearFocus();
                btCheckInManual.requestFocus();
                
                insertCheck(true);
                
            }
        });
        
    }
    
    
    private void insertCheck(boolean checkIn){
        
        CheckHora check = new CheckHora();            
        
        check.setAno(datePikcer1.getYear());
        check.setDataString(datePikcer1.getYear() + "-" + (datePikcer1.getMonth() + 1) + "-" + datePikcer1.getDayOfMonth() + " " + timePicker2.getCurrentHour() + ":" + timePicker2.getCurrentMinute());
        check.setDia(datePikcer1.getDayOfMonth());
        check.setHora(timePicker2.getCurrentHour());
        check.setMinuto(timePicker2.getCurrentMinute());
        check.setMes(datePikcer1.getMonth() + 1);
        check.setMilisegundos(datePikcer1.getDrawingTime());
        
        check.setCheckIn(checkIn);
        
        tableCheckHoras.insert(check);
        
        ComponentName thisWidget = new ComponentName(getBaseContext(), CheckWidget1Provider.class);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getBaseContext());
        int[] idsWids = appWidgetManager.getAppWidgetIds(thisWidget);
        
        final int N = idsWids.length;
        
        
        Toast.makeText(this, getString(R.string.checksalvo), Toast.LENGTH_SHORT).show();
        

        // Perform this loop procedure for each App Widget that belongs to this provider
        for (int i = 0; i < N; i++) {
            int appWidgetId = idsWids[i];                        
            
            RemoteViews views = new RemoteViews(getBaseContext().getPackageName(), R.layout.widget_1_layout);
            
            updateCheck(views);
            
            
            
            
            
            Intent intent = new Intent(getBaseContext(),UpdateWidgetService.class);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,idsWids[i]);            
            PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), appWidgetId, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
            
            views.setOnClickPendingIntent(R.id.btWidgetCheck, pendingIntent);
            
                                                         
            appWidgetManager.updateAppWidget(appWidgetId, views);
        }
        
    }
    
    private void updateCheck(RemoteViews views){
        if(tableCheckHoras.IsCheckInLast()){
            views.setTextViewText(R.id.txtWidgetCheck, "CheckOut");
            views.setImageViewResource(R.id.btWidgetCheck, R.drawable.checkoutwid);
        } else {
            views.setTextViewText(R.id.txtWidgetCheck, "CheckIn");
            views.setImageViewResource(R.id.btWidgetCheck, R.drawable.checkinwid);           
        }
    }
    
//    private void updateCheck(){
//        if(tableCheckHoras.IsCheckInLast()){
//            txtCheckManual.setText("CheckOut");
//            btCheckManual.setImageResource(R.drawable.checkout);
//        } else {
//            txtCheckManual.setText("CheckIn");
//            btCheckManual.setImageResource(R.drawable.checkin);           
//        }
//    }

}
